// Copyright [2020] <unknown>
#include <bits/stdc++.h>
using namespace std;
// #define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
#define dbg(x...) { cerr << "\033[32;1m" << #x << " -> "; err(x); }
void err() { cerr << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cerr << a << ' '; err(x...); }
#else
#define dbg(...)
#endif

#define sz(x) ((int)x.size())

typedef long long LL;

const int N = 405;

LL dp[N][N];

void update(LL &a, LL b) {
    if (a == -1 || a > b)
        a = b;
}

void update2(LL &a, LL b) {
    if (a == -1 || a < b)
        a = b;
}

int a[N];

int n, m;

LL sqr(int x) {
    return 1LL * x * x;
}

LL solve(int a, int b) {
    return a%b * sqr(a/b+1) + (b-a%b) * sqr(a/b);
}

int main(int argc, char const *argv[]) {
    // code

    scanf("%d%d", &n, &m);
    for (int i=1; i<=n; ++i)
        scanf("%d", &a[i]);
    memset(dp, -1, sizeof dp);

    // dp[0][0] = 0;
    // for (int i=1; i<=n; ++i)
    //     for (int j=1; j<=m; ++j) {
    //         for (int k=1; j-k>=i-1; ++k) {
    //             if (~dp[i-1][j-k])
    //                 update(dp[i][j], dp[i-1][j-k] + solve(a[i], k));
    //         }
    //     }
    // cout << dp[n][m] << endl;
    int q;

    int sum = 0;
    for (int i=1; i<=n; ++i)
        sum += a[i];
    scanf("%d", &q);

    for (int kk=0; kk<q; ++kk) {
        int x, v;
        scanf("%d%d", &x, &v);
        sum += v - a[x];
        a[x] = v;
        memset(dp, -1, sizeof dp);
        dp[0][0] = 0;
        for (int i=1; i<=n; ++i) {
            int stdv = (double)a[i] * m / sum;
            int l = max(1, stdv-6), r = min(m, stdv+6);
            for (int j=1; j<=m; ++j) {
                for (int k=l; k<=r; ++k) {
                    if (j-k>=0 && ~dp[i-1][j-k])
                        update(dp[i][j], dp[i-1][j-k] + solve(a[i], k));
                }
            }
        }

        printf("%lld\n", dp[n][m]);
    }
    return 0;
}
